<?php

function AutoHide_DisableExtension($ExtensionForm) {
   $FormContext = $ExtensionForm->Context;
   $ExtensionName = $ExtensionForm->DelegateParameters['ExtensionName'];
   if ($ExtensionName == "AutoHide"){
      // Remove the AutoHide permissions from all roles.
      $RoleManager = $FormContext->ObjectFactory->NewContextObject($FormContext, 'RoleManager');
      $RoleData = $RoleManager->GetRoles();
      while ($Row = $FormContext->Database->GetRow($RoleData)){
         $Role = $FormContext->ObjectFactory->NewContextObject($FormContext, 'Role');
	 $Role->Clear();
         $Role->GetPropertiesFromDataSet($Row);
	 // If the role doesn't contain either permission key, don't change it.
	 if ((array_key_exists('PERMISSION_AUTO_HIDE',$Role->Permissions))
	 	|| (array_key_exists('PERMISSION_MODERATE_AUTO_HIDES',$Role->Permissions))){
	    RemovePermissionKeyFromRole($Role, 'PERMISSION_AUTO_HIDE');
	    RemovePermissionKeyFromRole($Role, 'PERMISSION_MODERATE_AUTO_HIDES');
	    SaveRolePermissions($FormContext,$Role);
	 }
      }
   }
}

$Context->AddToDelegate('ExtensionForm','PreExtensionDisable',
	'AutoHide_DisableExtension');

?>
